define([
    'jQuery',
    'Underscore',
    'Backbone',
    'Kendo',
    'i18n!nls/language',
    'datasources/crown',
    'datasources/stem',
    'datasources/root',
    'datasources/health',
    'datasources/qualification'
], function($, 
            _, 
            Backbone, 
            Kendo, 
            i18n, 
            crownDatasource, 
            stemDatasource, 
            rootDatasource, 
            healthDatasource, 
            qualificationDatasource) {

    
    var investigationNewView = kendo.observable({
        
        // default values
        crowns: crownDatasource,
        stems: stemDatasource,
        roots: rootDatasource,
        healths: healthDatasource,
        qualifications: qualificationDatasource,
        
        crownValue: [],
        stemValue: [],
        rootValue: [],
        healthValue: 0,
        qualificationValue: 0,
        
        score: { 'crown':0, 'stem':0, 'root':0, 'health':0 },
        calculatedScore: 0,
        
                
        label: i18n.investigation.view.new,
        
        
        
        displayCrownValue: function() {
            
            return this.get("crownValue").join(", ") +" | score: "+ this.get("score").crown ;
            
        },
        
        
        
        displayStemValue: function() {
            
            return this.get("stemValue").join(", ") +" | score: "+ this.get("score").stem ;
            
        },
        
        
        
        displayRootValue: function() {
            
            return this.get("rootValue").join(", ") +" | score: "+ this.get("score").root ;
            
        },
        
        
        
        displayHealthValue: function() {
            
            return this.get("healthValue") +" | score: "+ this.get("score").health ;
            
        },
        
        
        
        displayCalculatedHealthValue: function() {
            
            return this.get("calculatedScore");
            
        },
        
        
        
        /**
         * function crownListener
         * Will calculcate the score for the selected crown items.
         * 
         * @author      Jeroen Sen <jeroensen@22vision.nl>
         * @version     20120915
         * @param       Event e
         * @return      void
         * 
         */
        crownListener: function(e) {
          
            var crowns = this.crowns.data();
            var selected = this.get("crownValue").toIntArray();
            var score = 0;
            

            for (var n in crowns) {
                
                if (typeof crowns[n] == "object" && crowns[n].id != undefined) {

                    if ($.inArray(crowns[n].get("id"), selected) != -1) {
                        score += crowns[n].get("value");
                    }

                }
                
            }

alert("crown score : "+ score);
            this.score.crown = score;
            
            this.calculateTotalScore();
            
        },
        
        
        
        /**
         * function stemListener
         * Will calculcate the score for the selected stem items.
         * 
         * @author      Jeroen Sen <jeroensen@22vision.nl>
         * @version     20120915
         * @param       Event e
         * @return      void
         * 
         */
        stemListener: function(e) {
          
            var stems = this.stems.data();
            var selected = this.get("stemValue").toIntArray();
            var score = 0;
            

            for (var n in stems) {
                
                if (typeof stems[n] == "object" && stems[n].id != undefined) {

                    if ($.inArray(stems[n].get("id"), selected) != -1) {
                        score += stems[n].get("value");
                    }

                }
                
            }

alert("stem score : "+ score);
            this.score.stem = score;
            
            this.calculateTotalScore();
            
        },
        
        
        
        /**
         * function rootListener
         * Will calculcate the score for the selected root items.
         * 
         * @author      Jeroen Sen <jeroensen@22vision.nl>
         * @version     20120915
         * @param       Event e
         * @return      void
         * 
         */
        rootListener: function(e) {
          
            var roots = this.roots.data();
            var selected = this.get("rootValue").toIntArray();
            var score = 0;
            

            for (var n in roots) {
                
                if (typeof roots[n] == "object" && roots[n].id != undefined) {

                    if ($.inArray(roots[n].get("id"), selected) != -1) {
                        score += roots[n].get("value");
                    }

                }
                
            }

alert("root score : "+ score);
            this.score.root = score;
            
            this.calculateTotalScore();
            
        },
        
        
        
        /**
         * function healthListener
         * 
         * 
         */
        healthListener: function(e) {
            
            var healths = this.healths.data();
            var health = this.get("healthValue");
            var score = 0;
            
console.log(healths);
console.log(health);

           
            for (var n in healths) {
                
                if (typeof healths[n] == "object" && healths[n].id != undefined) {
                    
                    if (healths[n].get("id") == health) {
                        score += healths[n].get("value");
                    }
                    
                }
                
            }

alert("health score : "+ score);
            this.score.health = score;
            
            this.calculateTotalScore();            
            
        },
        
        
        
        /**
         * function calculcateTotalScore
         * Will calculate the sum of all individual scores.
         * 
         * @author      Jeroen Sen <jeroensen@22vision.nl>
         * @version     20120916
         * @param       void
         * @return      Int
         * 
         */
        calculateTotalScore: function() {
            
            var total = this.get("score").crown + this.get("score").stem + this.get("score").root + this.get("score").health;
//            
//            
//            
console.log(this.get("score"));
//            for (var n in this.score) {
//console.log(n);
//console.log(this.score[n]);
//                total += this.score[n];
//            }
            
            
            this.set("calculatedScore", total);
            
            return total;
        
        },
        
        
        
        save: function() {

//            var Evelijn = this.get("checkboxListValue").join(", ");
            alert('opslaan => verleden tijd');
            
            return {};
            
        },
        
        
        renderSubviews: function() {
            
            this.retrieveData();
              
        },
        
        
        
        retrieveData: function() {
          
            crownDatasource.filter({ field: "active", operator: "eq", value: true });
            crownDatasource.sync();
            crownDatasource.read();
            
            stemDatasource.filter({ field: "active", operator: "eq", value: true });
            stemDatasource.sync();
            stemDatasource.read();
            
            rootDatasource.filter({ field: "active", operator: "eq", value: true });
            rootDatasource.sync();
            rootDatasource.read();
            
            healthDatasource.filter({ field: "active", operator: "eq", value: true });
            healthDatasource.sync();
            healthDatasource.read();
            
        },
        
        
        
        __construct: (function(self, window, undefined) {
            
            // do nothing for now...
            
        })(this, window)
        
                
        
    });
    
    
    
    return investigationNewView;
    
    
    
});